package com.njtoyo.taxi.admin.repository.secondary;

import com.njtoyo.taxi.entity.backend.AdminUser;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;

import java.util.List;

public interface AdminUserRepository extends JpaRepository<AdminUser, Integer> {

    AdminUser findByMobileNumber(String mobileNumber);

    AdminUser findByAccount(String account);

    @Query(value = "SELECT a.identity_id FROM admin_user_identity a WHERE a.admin_user_id = :adminId AND a.identity_category = :category",nativeQuery = true)
    List<Long> findIdentityIdsByAdminUserId(@Param("adminId") Integer adminId, @Param("category")Integer category);

    @Query(value =  "SELECT DISTINCT `name` FROM resource WHERE is_hidden = 0 AND id IN ( SELECT resource_id FROM role_resource_mapping WHERE role_id IN ( SELECT role_id FROM user_role_mapping WHERE user_id = :adminId ))", nativeQuery = true)
    List<String> findResourceNamesByAdminUserId(@Param("adminId") Integer adminId);
}
